'''
* This file is part of the Chinchon (https://notabug.org/alkeon/chinchon.
* Copyright (c) 2020 Alejandro "alkeon" Castilla.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
'''

from deck import Deck
from hand import Hand
from player import Player, debug
from random import shuffle
from copy import deepcopy

def is_int(string):
    try: 
        int(string)
        return True
    except ValueError:
        return False
'''
Check if one player has more than 99 points(loser player)
'''
def has_loser(points):
    i = 0
    is_loser = False
    while(i < len(points)):
        if(points[i] >= 100):
            is_loser = True
        print('Player ' + str(i) + ' has ' + str(points[i]) + ' points')
        i += 1
    return is_loser

'''
Distribute 7 cards to every player
'''
def distribute_cards(list_of_players, deck):
    position = 0
    new_list_of_players = list()

    while(position < len(list_of_players)):
        hand = Hand()
        i = 0

        while(i < deck.size() and i < 7):
                
            hand.insert(0, deck.get(i))
            i += 1

        while(i > 0):
            deck.pop(0)
            i -= 1

        new_list_of_players.append(Player(hand,
            list_of_players[position].get_mode(), list_of_players[position].get_hard_mode()))

        position += 1
    return new_list_of_players

def shuffle_reversed_cards(deck, reversed_deck):
    
    top_card = reversed_deck.get(0)
    deck.add_cards(reversed_deck.get_cards()[1:])
    deck.shuffle()
    reversed_deck.clear()
    reversed_deck.insert(0, top_card)

'''
Main algorithm for game.
One time flag is used for limit how many games will be played
'''
def original(one_time):

    is_hard_mode, number_of_players, list_of_players = game_options()

    points = [0] * number_of_players

    is_first_game = True
    if not one_time:
        is_first_game = False

    i = 0
    while(not has_loser(points) and one_time == is_first_game):

        deck = Deck()
        card = deck.get_top_and_pop()
        list_of_one_card = list()
        list_of_one_card.append(card)
        player_with_cards = list()
        reversed_deck = Deck(list_of_one_card)

        player_with_cards = distribute_cards(list_of_players, deck)

        list_of_players = deepcopy(player_with_cards)
        selected_number_player = 0
        choice = 0
        while(choice != 2 and selected_number_player < len(list_of_players)):

            print(reversed_deck.get(0))
            player = list_of_players[selected_number_player]

            if(player.mode == 2 or debug):
                print('your hand')
                print(player)
                print(player.return_hand().calculate_points())

            choice = player.choose_option(deck, reversed_deck)

            if(choice != 2):
                i += 1
            
            if(deck.size() <= 5):
                shuffle_reversed_cards(deck, reversed_deck)

            selected_number_player += 1
            if(selected_number_player >= len(list_of_players)):
                selected_number_player = 0

        iterator = 0
        while(iterator < len(list_of_players)):
        
            print(list_of_players[iterator].return_hand())
            points[iterator] += list_of_players[iterator].return_hand().calculate_points()
            iterator += 1

        if one_time:
            is_first_game = False

'''
Ask user AI difficulty, number of players and which players are human or AI.
'''
def game_options():
    is_hard_mode_string = input('0.- Easy mode\n1.- Hard mode\n')
    is_hard_mode = int()
    if(is_int(is_hard_mode_string)):
        is_hard_mode = int(is_hard_mode_string)
    else:
        is_hard_mode = 0

    player_string = input('Number of players (max 6) ')
    number_of_players = int()

    if(is_int(player_string)):
        number_of_players = int(player_string)
    else:
        number_of_players = 2

    if(number_of_players > 6 or number_of_players < 2): number_of_players = 6

    list_of_players = list()
    iterator = 0
    while(iterator < number_of_players):
        hand = Hand()
        string = input('1.- AI\n2.- Human\n')
        if(is_int(string)):
            list_of_players.append(Player(hand, int(string), is_hard_mode))
        else:
            list_of_players.append(Player(hand, 2, is_hard_mode))

        iterator += 1

    return is_hard_mode, number_of_players, list_of_players

if __name__ == "__main__":
    decision_s = input('1.- One time game\n2.- Original\n')
    decision = int()
    if(is_int(decision_s)):
        decision = int(decision_s)
    else:
        decision = 1

    if(decision == 1):
        original(True)
    else:
        original(False)
